﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using JustinAngel.Neurons.Framework;
using Microsoft.Phone.Controls;
using JustinAngel.Neurons.Framework.Helpers;

namespace JustinAngel.Neurons.Pages.WebBrowser
{
    public partial class WebBrowserPage : PhoneApplicationPage
    {

        public WebBrowserPage()
        {
            InitializeComponent();
            this.webBrowser.Background = PanoramaBackground;
            this.Loaded += new RoutedEventHandler(WebBrowserPage_Loaded);
            webBrowser.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(webBrowser_Navigated);
            //IoC.Get<IMessenger>().Register<NavigateWebBrowserToMessage>(this,
            //    msg => _nextLoadUri = msg.Content);
            //webBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(webBrowser_LoadCompleted);
        }

        //private Uri _nextLoadUri;
        //void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        //{
        //    if (_nextLoadUri != null)
        //    {
        //        webBrowser.Navigate(_nextLoadUri);
        //        _nextLoadUri = null;
        //    }
        //}

        void webBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            DataContext.As<WebBrowserViewModel>().CurrentSource = webBrowser.Source;
        }

        void WebBrowserPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.DataContext == null)
            {
                var vm = new ViewModelLocator().WebBrowserViewModel;
                if (this.Tag != null)
                {
                    vm.Data = this.Tag;
                }
                this.DataContext = vm;
            }
        }

        public ImageBrush PanoramaBackground
        {
            get
            {
                return new ImageBrush()
                {
                    ImageSource = new BitmapImage(PanoramaBackgroundImageSource),
                    Stretch = Stretch.None
                };
            }
        }

        public Uri PanoramaBackgroundImageSource
        {
            get
            {
                Visibility PhoneLightThemeVisibility = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];
                if (PhoneLightThemeVisibility == Visibility.Collapsed)
                {
                    return new Uri("/PanoramaBackgroundNeuron.jpg", UriKind.Relative);
                }
                else
                {
                    return new Uri("/PanoramaBackgroundNeuronLight.jpg", UriKind.Relative);
                }
            }
        }
    }
}